package com.ruix.app.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.ui.stratum.ReleasePostsActivity;
import com.ruix.app.util.SoftInputUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;

/**
 * Created by hadexs on 2017/7/3.
 */

public class ReleasePostTagView extends LinearLayout implements View.OnClickListener{

    private View tagsContain;
    private TagFlowLayout tagFlowLayout;
    private ArrayList<String> tags = new ArrayList<>();
    protected Activity activity;
    private SharedPreferences sp;
    private static final String SHARE_PREFERENCE_TAG = "soft_input_height";
    private static final String SHARE_PREFERENCE_NAME = "com.emotioninputdetector";
    private TagAdapter tagAdapter;
    private int defaltHeight;
    private ReleasePostTagViewListener listener;

    public ReleasePostTagView(Context context) {
        super(context);
        initView(context);
    }

    public ReleasePostTagView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ReleasePostTagView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
    private void initView(Context context) {
        this.activity = (Activity) context;
        LayoutInflater.from(context).inflate(R.layout.layout_view_release_post_tag, this, true);
        tagsContain = findViewById(R.id.ll_tags_contain);
        tagsContain.setVisibility(View.GONE);
        tagFlowLayout = (TagFlowLayout)findViewById(R.id.id_flowlayout);
        sp = activity.getSharedPreferences(SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
        defaltHeight = (int)context.getResources().getDimension(R.dimen.h_w_210);
        setLisenler();
    }


    private void setLisenler(){
        findViewById(R.id.ll_add_pic_button).setOnClickListener(this);
        findViewById(R.id.ll_add_tag_button).setOnClickListener(this);
        findViewById(R.id.tv_add_tag).setOnClickListener(this);
        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (tags != null && position< tags.size() ) {
                    String tag = tags.get(position);
                    if (listener != null){
                        listener.addTag(tag);
                    }
//                    addTag(tag);
                }
                return true;
            }
        });
    }

    public void addTags(ArrayList<String> tags){
        this.tags = tags;
        tagAdapter = new TagAdapter<String>(tags) {
            final LayoutInflater mInflater = LayoutInflater.from(activity);

            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.item_post_tagts,
                        tagFlowLayout, false);
                tv.setText(s);
                return tv;
            }
        };
        tagFlowLayout.setAdapter(tagAdapter);
        setTagsContainHeight();
    }

    private void setTagsContainHeight(){
        int softInputHeight = getSupportSoftInputHeight();
        if (softInputHeight == 0) {
            softInputHeight = sp.getInt(SHARE_PREFERENCE_TAG, defaltHeight);
        }
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)tagsContain.getLayoutParams();
        layoutParams.height = softInputHeight;
        tagsContain.setLayoutParams(layoutParams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_add_pic_button:
                if (listener != null){
                    listener.clickAddPhotoView();
                }
                break;
            case R.id.ll_add_tag_button:
                tagsContain.setVisibility(View.VISIBLE);
                if (listener != null){
                    listener.clickAddTagView();
                }

//                SoftInputUtil.hideSoftInput(inputEditText); //隐藏输入框
                break;

            case R.id.tv_add_tag:
                if (listener != null){
                    listener.addNewTag();
                }
//                addNewTag();
//                showSoftInput();
                break;

        }
    }

    public void hideTagsContain(){
        tagsContain.setVisibility(View.GONE);
    }

    private boolean isSoftInputShown() {
        return getSupportSoftInputHeight() != 0;
    }

    private int getSupportSoftInputHeight() {
        Rect r = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
        int screenHeight = activity.getWindow().getDecorView().getRootView().getHeight();
        int softInputHeight = screenHeight - r.bottom;
        if (Build.VERSION.SDK_INT >= 20) {
            // When SDK Level >= 20 (Android L), the softInputHeight will contain the height of softButtonsBar (if has)
            softInputHeight = softInputHeight - getSoftButtonsBarHeight();
        }
        if (softInputHeight < 0) {
            //
            Log.w("EmotionInputDetector", "Warning: value of softInputHeight is below zero!");
        }
        if (softInputHeight > 0) {
            sp.edit().putInt(SHARE_PREFERENCE_TAG, softInputHeight).apply();
        }
        return softInputHeight;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private int getSoftButtonsBarHeight() {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight) {
            return realHeight - usableHeight;
        } else {
            return 0;
        }
    }


    public void setListener(ReleasePostTagViewListener listener) {
        this.listener = listener;
    }

    public interface  ReleasePostTagViewListener{
        void clickAddPhotoView();
        void clickAddTagView();
        void addNewTag();
        void addTag(String tag);
    }
}
